/*******************************************************************************
 * Copyright（C） CETC-32
 * @file:SSOProperties.java
 * @Description：
 * @Author ：徐文远
 * @version：1.0
 * @date ： 2021/5/7 下午3:14
 ******************************************************************************/

package com.cetc32.webutil.client.bean;

import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@Scope("singleton")
@ConfigurationProperties(value = "sso")
public class SSOProperties {
    Logger logger = LoggerFactory.getLogger(SSOProperties.class);
    /**
     * 校验tokenUrl
     * */
    @Value("${sso.verifyUrl}")
    @NotNull(message = "SSO验证地址不能为空，请确保配置文件中 sso.verifyUrl不为空")
    String verifyUrl;

    /**
     * SSO 登陆页面Url
     * */
    @Value("${sso.passportUrl}")
    @NotNull(message = "SSO登陆地址不能为空，请确保配置文件中 sso.passportUrl不为空")
    @NotNull
    String passportUrl;
    /**
     * SSO 接口时候默认要求登陆
     * 默认为否
     * **/
    boolean defaultLoginRequiredEnable = false;
    /**
     * 登陆校验的url
     * **/
    List<String> loginInclude;
    /**
     * 登陆校验不校验URL
     * **/
    List<String> loginExclude;
    /***
     * 当前系统用户URL 访问权限控制
     * ***/
    String permission;
    /**
     * 当前系统名称
     * **/
    String sysname;
    /**
     * 更新用户角色列表的请求地址
     * */
    String registerUrl;
    /**
     * 当前系统的URL
     * **/
    String sysurl;

    /**
     * 当前系统编码
     * */
    String syscode;

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getSysname() {
        return sysname;
    }

    public void setSysname(String sysname) {
        this.sysname = sysname;
    }

    public String getRegisterUrl() {
        return registerUrl;
    }

    public void setRegisterUrl(String registerUrl) {
        this.registerUrl = registerUrl;
    }

    public String getSysurl() {
        return sysurl;
    }

    public void setSysurl(String sysurl) {
        this.sysurl = sysurl;
    }

    public String getVerifyUrl() {
        return verifyUrl;
    }

    public void setVerifyUrl(@NotNull String verifyUrl) {
        this.verifyUrl = verifyUrl;
    }

    public String getPassportUrl() {
        return passportUrl;
    }

    public void setPassportUrl(@NotNull String passportUrl) {
        this.passportUrl = passportUrl;
    }

    public boolean getDefaultLoginRequiredEnable() {
        return defaultLoginRequiredEnable;
    }

    public void setDefaultLoginRequiredEnable(boolean defaultLoginRequiredEnable) {
        this.defaultLoginRequiredEnable = defaultLoginRequiredEnable;
    }

    public List<String> getLoginInclude() {
        return loginInclude;
    }

    public void setLoginInclude(String loginInclude) {
        if(StringUtils.isBlank(loginInclude))
            return;
        this.loginInclude=Arrays.asList(loginInclude.split(","));

    }

    public List<String> getLoginExclude() {
        return loginExclude;
    }

    public void setLoginExclude(String loginExclude) {
        if(StringUtils.isBlank(loginExclude))
            return;
        this.loginExclude = Arrays.asList(loginExclude.split(","));
    }

    public boolean isLoginSkipped(String url){
        return isUrlMatched(url, loginExclude);
    }

    public boolean isLoginRequired(String url){
        return isUrlMatched(url, loginInclude);
    }

    private boolean isUrlMatched(String url, List<String> loginInclude) {
        if(loginInclude ==null || loginInclude.size()==0)
            return false;
        logger.debug("isMatched：{}",url);
        for(String s: loginInclude){
            if(s.endsWith("**")){
                s=s.substring(0,s.indexOf("**"));
                logger.debug("isMatched **：{}",s);
                if(url.startsWith(s)){
                    return true;
                }

            }else if(s.endsWith("*")){
                logger.debug("isMatched *：{}",s);
                s=s.substring(0,s.indexOf("*"));
                if(!url.replace(s,"").contains("/")){
                    return true;
                }
            }else {
                if(s.equals(url))
                    return true;
            }
        }
        return false;
    }

    public String getSyscode() {
        return syscode;
    }

    public void setSyscode(String syscode) {
        this.syscode = syscode;
    }
}
